package com.study.controller;

import com.study.pojo.Book;
import com.study.service.BookService;
import com.study.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @Author Miss kun
 * @Date 2020/9/29 19:08
 * @Version 1.0
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private ChapterService chapterService;


    /**
     * 获取指定图书的id和章节id的信息
     * @param bookId
     * @param chapterId
     * @param model
     * @return
     */
    @RequestMapping("/getChapter/{bookId}/{chapterId}")
    public String getChapterById(@PathVariable String bookId, @PathVariable String chapterId, Model model){
        System.out.println("getChapterById------>bookId:"+bookId+"chapterId:"+chapterId);
        Book bookChapter = bookService.getChapterById(Integer.parseInt(bookId), Integer.parseInt(chapterId));
        System.out.println(bookChapter+"\n");
        model.addAttribute("bookChapter",bookChapter);
        return "chapterText";
    }

    /**
     * 查询所有的书籍
     */
  /*  @RequestMapping("/allBook")
    public String showAllBooks(Model model){
        List<Books> books = bookService.queryByAllBooks();
        model.addAttribute("books",books);
        return "showbooks";
    }*/

    /**
     * 实现跳转添加图书页面
     */
/*    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addbook";
    }*/

    /**
     * 添加图书处理
     */
/*    @RequestMapping("/addBook")
    public String addBook(Books book){
        System.out.println("addBook===》"+book);
        bookService.addBook(book);
        return "redirect:/book/allBook";
    }*/
    /**
     * 转发到修改页面
     */
/*    @RequestMapping("/doUpdateBook")
    public String doUpdateBook(@RequestParam("bookID") Integer id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBook",books);
        return "updateBook";
    }*/

    /**
     * 修改图书
     */
/*    @RequestMapping("/updateBook")
    public String updateBook(Books book){
        System.out.println("updateBook------>"+book);
        bookService.updateBook(book);
        return "redirect:/book/allBook";
    }*/

    /**
     * 删除图书使用restFul风格
     */
/*    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") Integer id){
        System.out.println("deleteBook----->"+id);
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }*/


    /**
     * 查询图书
     */
/*    @RequestMapping("/searchBook")
    public String searchBookByName(@RequestParam("bookName") String name, Model model){
        System.out.println("searchBookByName----->"+name);
        List<Books> bookList = bookService.queryBookByName(name);
        if (bookList.size() == 0){
            bookList = bookService.queryByAllBooks();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("books",bookList);
        return "showbooks";
    }*/


}
